package com.zrrd.gmall.webll.controller;

import com.zrrd.gmall.cart.fegin.CartFeginClient;
import com.zrrd.gmall.model.product.SkuInfo;
import com.zrrd.gmall.product.fegin.ProductFeginClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;

@Controller
public class CartController {

    @Autowired
    private CartFeginClient cartFeginClient;
    @Autowired
    private ProductFeginClient productFeginClient;

    //查询购物车
    @RequestMapping("/cart.html")
    public String index(HttpServletRequest request){
        return "cart/index";
    }

    //添加购物车
    @RequestMapping("/addCart.html")
    public String addCart(@RequestParam("skuId") Long skuId,
                          @RequestParam("skuNum") Integer skuNum,
                          HttpServletRequest request) {
        //调用fegin请求添加购物车
        cartFeginClient.addToCart(skuId, skuNum);
        //前端页面渲染需要一个skuInfo
        SkuInfo skuInfo = productFeginClient.getSkuInfo(skuId);
        //存入到请求域当中
        request.setAttribute("skuInfo", skuInfo);
        request.setAttribute("skuNum", skuNum);
        //跳转页面
        return "cart/addCart";
    }
}
